from meta import get_Session

GlobalSession = get_Session()

class SqlAlchemySession(object):
    """
    For each requets global session is used and set
    as 'sa' attribute. If some error occurs rollback
    would be called - otherwise session is commited.
    """
    def process_request(self, request):
        request.sa = get_Session()

    def process_exception(self, request, exception):
        session = getattr( request, 'sa', None )
        if session is not None:
            session.close()

    def process_response(self, request, response):
        session = getattr( request, 'sa', None )
        if session is not None:
            session.commit()
            session.close()
        return response
